package com.link.base.base.project.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.link.base.base.project.model.ProjectLossCompare;
import com.link.core.basic.annotations.JsonParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.base.base.project.model.ProjectLoss;
import com.link.base.base.project.service.ProjectLossService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目失标
 *
 * @author 王昊
 * @date 2020-10-28 14:52:29
 */
@Controller
@RequestMapping("/link/projectLoss")
public class ProjectLossController extends BasicController<ProjectLoss> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ProjectLossService projectLossService;

    @Override
    public BasicService<ProjectLoss> getBasicService() throws Exception {
        return projectLossService;
    }

    /**
     * 项目失标报表
     * @param entity
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/lossReasonReport",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> lossReasonReport(@JsonParam ProjectLoss entity, HttpSession session, HttpServletRequest request) {
        Map<String,Object> result = new HashMap<>(4);
        try {
            final List<ProjectLoss> projectLosses = projectLossService.queryAllReasonNum(entity);
            result.put("success",true);
            //将cha返回
            result.put("newRow",projectLosses);
        } catch (Exception e) {
            result.put("success",false);
            result.put("result",e.getMessage());
        }
        return result;
    }


    /**
     * 项目失标报表
     * @param entity
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/compare",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> compare(@JsonParam ProjectLoss entity, HttpSession session, HttpServletRequest request) {
        Map<String,Object> result = new HashMap<>(4);
        try {
            ProjectLoss projectLoss = projectLossService.queryById(entity);
            if (null == projectLoss) {
                throw new RuntimeException("数据丢失，请联系管理员!");
            }
            List<ProjectLossCompare> listData = new ArrayList<>();
            ProjectLossCompare titleObj = new ProjectLossCompare();
            titleObj.setTitle("客户名称");
            titleObj.setId("1");
            titleObj.setList(new String[]{"宁波一彬电子科技股份有限公司", projectLoss.getCompetitorName()});
            listData.add(titleObj);

            ProjectLossCompare vehicleTypeObj = new ProjectLossCompare();
            vehicleTypeObj.setTitle("车型");
            vehicleTypeObj.setId("2");
            vehicleTypeObj.setList(new String[]{projectLoss.getVehicleType(), projectLoss.getVehicleType()});
            listData.add(vehicleTypeObj);

            ProjectLossCompare comptypeNameObj = new ProjectLossCompare();
            comptypeNameObj.setTitle("零件类型");
            comptypeNameObj.setId("3");
            comptypeNameObj.setList(new String[]{projectLoss.getComptypeName(), projectLoss.getComptypeName()});
            listData.add(comptypeNameObj);

            ProjectLossCompare quoteObj = new ProjectLossCompare();
            quoteObj.setTitle("报价");
            quoteObj.setId("4");
            quoteObj.setList(new String[]{String.valueOf(projectLoss.getCustPrice()), String.valueOf(projectLoss.getCompetitorQuote())});
            if (!projectLoss.getCustPrice().equals(projectLoss.getCompetitorQuote())) {
                quoteObj.setIsEqual("N");
            }

            listData.add(quoteObj);

            ProjectLossCompare mouldCostSumObj = new ProjectLossCompare();
            mouldCostSumObj.setTitle("磨具报价");
            mouldCostSumObj.setId("5");
            mouldCostSumObj.setList(new String[]{String.valueOf(projectLoss.getMouldCostSum()), String.valueOf(projectLoss.getCompMouldPrice())});
            if (!projectLoss.getMouldCostSum().equals(projectLoss.getCompMouldPrice())) {
                mouldCostSumObj.setIsEqual("N");
            }
            listData.add(mouldCostSumObj);

            result.put("success",true);
            result.put("listData",listData);
        } catch (Exception e) {
            result.put("success",false);
            result.put("result",e.getMessage());
        }
        return result;
    }
}
